package Operation;

import Student.Student;

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-04-16
 * Time: 20:39
 */
public class MofityOperation implements IOperation{

    @Override
    public void work(TreeMap<Student,String> treeMap) {
        System.out.println("编辑信息");
        String name=null;
        String id=null;
        int age=0;
        String tel=null;
        String sex=null;
        Scanner scanner=new Scanner(System.in);
        Iterator<Student> iterator=treeMap.keySet().iterator();
        System.out.println("请输入你要编辑人的姓名");
        String name1=scanner.nextLine();
        while(iterator.hasNext()){
            Student key=iterator.next();
            if(name1.equals(key.getName())){
                System.out.println("信息如下");
                System.out.println(key);
                while(true){
                    System.out.println("请输入学号，学号为10个数字");
                    id=scanner.nextLine();
                    if(checkId(id)){
                        System.out.println("输入成功");
                    }else{
                        System.out.println("输入格式有误,请全部重新输入");
                        continue;
                    }
                    System.out.println("请输入姓名");
                    name=scanner.nextLine();
                    if(checkName(name)){
                        System.out.println("输入成功");
                    }else{
                        System.out.println("输入格式有误,请全部重新输入");
                        continue;
                    }
                    System.out.println("请输入性别");
                    sex=scanner.nextLine();
                    if(checkSex(sex)){
                        System.out.println("输入成功");
                    }else{
                        System.out.println("输入格式有误,请全部重新输入");
                        continue;
                    }
                    System.out.println("请输入年龄");
                    age=scanner.nextInt();
                    if(checkAge(age)){
                        System.out.println("输入成功");
                    }else{
                        System.out.println("输入格式有误,请全部重新输入");
                        continue;
                    }
                    System.out.println("请输入手机号");
                    Scanner scanner1=new Scanner(System.in);
                    tel=scanner1.nextLine();
                    if(checkTel(tel)){
                        System.out.println("输入成功");
                    }else{
                        System.out.println("输入格式有误,请全部重新输入");
                        continue;
                    }
                    break;
                }
                key.setId(id);
                key.setName(name);
                key.setAge(age);
                key.setSex(sex);
                key.setTel(tel);
            }
        }
        System.out.println("查询无此人");
    }
    public boolean checkId(String str){
        return str.matches("^[0-9]{10}$");
    }
    public boolean checkName(String str){
        return str.matches("^[\u4E00-\u9FA5]{2,4}$");
    }
    public boolean checkAge(int age){
        if(age>=5&&age<=100)
            return true;
        else
            return false;
    }
    public boolean checkTel(String str){
        return str.matches("^[0-9]{11}$");
    }
    public boolean checkSex(String str){
        if(str.length()!=1)
            return false;
        if(str.equals("男")||str.equals("女")){
            return true;
        }
        else
            return false;
    }
}
